package com.ss.android.newmedia.webview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.ss.android.common.app.permission.PermissionsManager;

import java.io.File;
import java.lang.ref.WeakReference;

public class BaseWebChromeClient extends WebChromeClient {
    ValueCallback mFilePathCallback;
    boolean b;
    private ValueCallback mValueCallback;
    private WeakReference<Fragment> mFragmentRef;
    private WeakReference<Activity> mActivityRef;
    private String f;

    public BaseWebChromeClient() {
    }

    public BaseWebChromeClient(Fragment fragment) {
        this.mFragmentRef = new WeakReference(fragment);
        if (fragment.getActivity() != null) {
            this.mActivityRef = new WeakReference(fragment.getActivity());
        }
    }

    public BaseWebChromeClient(Activity activity) {
        this.mActivityRef = new WeakReference(activity);
    }

    static Intent createGetContentIntent(String type) {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.addCategory("android.intent.category.OPENABLE");
        intent.setType(type);
        return intent;
    }

    static Intent createChooserIntent(Intent[] intents) {
        Intent intent = new Intent("android.intent.action.CHOOSER");
        intent.putExtra("android.intent.extra.INITIAL_INTENTS", intents);
        return intent;
    }

    private void a(String arg2, String arg3) {
        if (arg2 == null) {
            arg2 = "";
        }

        this.a(arg2.split(";"), arg3);
    }

    private void a(String[] arg9, String arg10) {
        int v7 = 2;
        int v1 = 0;
        try {
            String v0_1 = "filesystem";
            String v2 = arg9[0];
            if (!TextUtils.isEmpty(arg10)) {
                v0_1 = arg10;
            }

            if (arg10.equals("filesystem")) {
                int v3 = arg9.length;
                while (v1 < v3) {
                    String[] v4 = arg9[v1].split("=");
                    if (v4.length == v7 && ("capture".equals(v4[0]))) {
                        v0_1 = v4[1];
                    }

                    ++v1;
                }
            }

            String v1_1 = v0_1;
            this.f = null;
            if ((v2.equals("image/*")) || (v2.equals("video/*")) || (v2.equals("audio/*"))) {
                PermissionsManager.getInstance().requestPermission(this.mActivityRef.get(), new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"}, new c(this, v2, v1_1));
            } else {
                this.a(this.a());
            }
            return;
        } catch (Exception v0) {
            v0.printStackTrace();
            return;
        }
    }

    final void a(Intent arg3) {
        int v1 = 2048;
        if (this.mFragmentRef != null && this.mFragmentRef.get() != null) {
            this.mFragmentRef.get().startActivityForResult(arg3, v1);
        } else if (this.mActivityRef != null && this.mActivityRef.get() != null) {
            this.mActivityRef.get().startActivityForResult(arg3, v1);
        }
    }

    final Intent a() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.addCategory("android.intent.category.OPENABLE");
        intent.setType("*/*");
        Intent intent1 = createChooserIntent(new Intent[]{this.b(), createVideoCaptureIntent(), createRecordSoundIntent()});
        intent1.putExtra("android.intent.extra.INTENT", intent);
        return intent1;
    }

    public final void onActivityResult(int arg7, int arg8, Intent arg9) {
        File v2;
        int v3 = -1;
        ValueCallback v1 = null;
        if (arg7 == 2048) {
            if (arg8 == 0 && (this.b)) {
                this.b = false;
                return;
            }

            if (Build.VERSION.SDK_INT < 21 || this.mFilePathCallback == null) {
                if (this.mValueCallback == null) {
                    this.b = false;
                    return;
                }

                Uri v0_1 = arg9 == null || arg8 != v3 ? ((Uri) v1) : arg9.getData();
                if (v0_1 == null && arg9 == null && arg8 == v3) {
                    v2 = new File(this.f);
                    if (v2.exists()) {
                        v0_1 = Uri.fromFile(v2);
                        this.getContext().sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", v0_1));
                    }
                }

                this.mValueCallback.onReceiveValue(v0_1);
                this.mValueCallback = v1;
            } else {
                Uri[] v0 = WebChromeClient.FileChooserParams.parseResult(arg8, arg9);
                if (v0 == null) {
                    v2 = new File(this.f);
                    if (v2.exists()) {
                        Uri v2_1 = Uri.fromFile(v2);
                        this.getContext().sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", v2_1));
                        v0 = new Uri[]{v2_1};
                    }
                }

                this.mFilePathCallback.onReceiveValue(v0);
                this.mFilePathCallback = v1;
            }

            label_34:
            this.b = false;
        }
    }

    final Intent b() {
        Intent v0 = new Intent("android.media.action.IMAGE_CAPTURE");
        File v2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + File.separator + "browser-photos");
        v2.mkdirs();
        this.f = v2.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
        v0.putExtra("output", Uri.fromFile(new File(this.f)));
        return v0;
    }

    static Intent createVideoCaptureIntent() {
        return new Intent("android.media.action.VIDEO_CAPTURE");
    }

    static Intent createRecordSoundIntent() {
        return new Intent("android.provider.MediaStore.RECORD_SOUND");
    }

    private Context getContext() {
        if (this.mFragmentRef == null || this.mFragmentRef.get() == null) {
            if (this.mActivityRef != null && this.mActivityRef.get() != null) {
                return this.mActivityRef.get();
            }
        } else {
            return this.mFragmentRef.get().getActivity();
        }
        return null;
    }

    public View getVideoLoadingProgressView() {
        int v4 = -1;
        int v3 = -2;
        Context context = this.getContext();
        if (context != null) {
            FrameLayout frameLayout = new FrameLayout(context);
            frameLayout.setLayoutParams(new ViewGroup.LayoutParams(v4, v4));
            ProgressBar progressBar = new ProgressBar(context);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(v3, v3);
            layoutParams.gravity = 17;
            frameLayout.addView(progressBar, layoutParams);
            return frameLayout;
        } else {
            return null;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        if (Build.VERSION.SDK_INT < 21) {
            this.mFilePathCallback = filePathCallback;
            this.a(fileChooserParams.getAcceptTypes(), "");
            return true;
        } else {
            return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
        }
    }

    public void openFileChooser(ValueCallback valueCallback) {
        this.mValueCallback = valueCallback;
        this.a("", "");
    }

    public void openFileChooser(ValueCallback valueCallback, String arg3) {
        this.mValueCallback = valueCallback;
        this.a(arg3, "");
    }

    public void openFileChooser(ValueCallback valueCallback, String arg2, String arg3) {
        this.mValueCallback = valueCallback;
        this.a(arg2, arg3);
    }
}

